<template>
  <div id="userSex-container">
    <van-picker title="更新性别" show-toolbar :columns="columns" :default-index="value" @confirm="onConfirm" @cancel="$emit('close')" />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
import { Toast } from 'vant'

export default {
  name: 'userSex',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  created () {},
  methods: {
    async onConfirm (value, index) {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '更新中....'
      })
      try {
        await updateUserProfile({
          gender: index
        })
        Toast.success('更新成功')
        this.$emit('input', value)
        this.$emit('close')
      } catch (err) {
        Toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
